<template>
  <div class="container">
    <h1>
      农田管理
      <el-button type="primary" @click="handleAdd" >
        <el-icon :size="size" :color="color">
          <Plus />
        </el-icon>新增农田
      </el-button>
    </h1>
    <el-table :data="farmData" style="width: 100%">
      <el-table-column prop="id" label="农田ID" width="120" />
      <el-table-column prop="farmland_name" label="农田名称" />
      <el-table-column prop="farmland_type" label="农田作物" />
      <el-table-column prop="manager_id" label="负责人" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button v-if="type === 'soil'" size="small" @click="handleView(scope.row)">查看</el-button>
          <el-button-group v-else>
            <el-button size="small" type="primary" @click="handleView(scope.row)">查看</el-button>
            <el-button size="small" type="info" @click="handleModify(scope.row)">修改</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <el-dialog v-model="openModify" title="新增农田" width="500">
    <el-form :model="form" label-width="auto" style="max-width: 600px">
    <el-form-item label="农田名称">
      <el-input v-model="form.name" />
    </el-form-item>
    <el-form-item label="农田作物">
      <el-input v-model="form.type" />
    </el-form-item>
    <el-form-item label="负责人">
      <el-input v-model="form.manager_id" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit(true,form.change)">提交</el-button>
      <el-button @click="onSubmit(false)">取消</el-button>
    </el-form-item>
  </el-form>
  </el-dialog>
</template>

<script setup>
import { ref ,onMounted,computed,watch,reactive } from 'vue'
import { useRouter,useRoute } from 'vue-router'
import { addFarmland, deleteFarmland, getAllFarmland, modifyFarmland } from '@/api/farmland'

// 是否打开修改界面
let openModify = ref(false)
const route = useRoute()
const router = useRouter()

const farmData = ref([
])
const loading = ref(false)
console.log(route.params.type)
const type = ref('')
// 表单数据
const form = reactive({
  id:'',
  name: '',
  type:'',
  manager_id:'',
  change:''
})
type.value = computed(()=>{
  return route.params.type
})
const getFarmland = async () => {
try {
  loading.value = true
  let response = await getAllFarmland()
  // farmData.value = response.data
  // 确保返回的数据是数组格式
  // console.log(response)
  response = response.data[0]
  // console.log(response)
  farmData.value = Array.isArray(response) ? response : [response]
} catch (error) {
  console.error('获取农田数据失败:', error)
  // ElMessage.error('获取农田数据失败')
} finally {
  loading.value = false
}
}
const onSubmit = async (state,type) => {
  if(state){
    if(type === "add"){
      let data = {
        name: form.name,
        type:form.type,
        manager_id:form.manager_id,
      }
      await addFarmland(data)
    }else{
      let data = {
        id: form.id,
        name: form.name,
        type:form.type,
        manager_id:form.manager_id,
      }
      await modifyFarmland(data)
    }
  }
  form.name = ''
  form.type = ''
  form.manager_id = ''
  await getFarmland()
  openModify.value = false
}
onMounted(() => {
  getFarmland()
})
watch(() => route.params.type,(newType) => {
  console.log('type change!' + newType)
  type.value = newType
})

const handleView = (row) => {
  console.log('Clicked row:', row); // 添加调试信息
  console.log('type:' + type.value)
  if(type.value === 'soil'){
    router.push(`/soil/${row.id}`)
  }else{
    router.push(`/pest/${row.id}`)
  }
}
const handleModify = (row) => {
  form.name = row.farmland_name
  form.type = row.farmland_type
  form.manager_id = row.manager_id
  form.change = 'modify'
  form.id = row.id
  openModify.value = true
}

const handleAdd = () => {
  openModify.value = true
  form.change = 'add'
}
const handleDelete = async (row) => {
  await deleteFarmland(row.id)
  await getFarmland()
}
</script>

<style scoped>
.container {
  padding: 20px;
}
h1 {
  margin-bottom: 20px;
}
</style>